<?php

namespace App\Logics;


use App\Models\SiteRss;
use App\Models\User;
use App\Service\Base\Logic;

class EmailLogic extends Logic
{

    // 社区：文章

    // 列表查询
    public function onLists()
    {
        $opt     = $this->option();
        $page    = $opt->getData('page', 1, 'intval');          // 获取热门等不需要翻页
        $rows    = $opt->getData('rows', 15, 'intval');
        $keyword = $opt->getData('keyword', null, 'trim');
        $orderBy = "id DESC";

        // 模型 和 查询  ->isCache($this->listCache)
        $where = [['status', '=', 1]];
        $bang  = ['verify',];
        if($keyword){
            $where[] = $this->orWhere('email', 'like', $keyword);
            $bang    = false;
        }

        // 获取所有的文章的时候 第一页不展示突发的top新闻
        $model  = new SiteRss();
        $args   = compact('where', 'page', 'rows', "bang", "orderBy");
        $KCache = $model->cacheKey('listspage', $args);
        $result = $model->cacheClass()->getCache($KCache);
        // 检查是否已经绑定
        if(($result) && (!empty($result['list']))){
            $model->checkBangList($result['list'], $KCache);
        }


        // 执行查询
        if(empty($result)){
            // 查询统计  统计必须在查询之前之前，不然只统计查询到的数据
            $query = $model->where($where)->orderByRaw($orderBy);
            $count = $query->count();
            if($page){
                $lists = $query->forPage($page, $rows)->get()->toArray();
            } else{
                $lists = $query->limit($rows)->get()->toArray();
            }

            // 组合
            $result = ['rows' => $rows, 'list' => $lists, 'page' => $page, 'count' => $count];
            if($bang){
                $model->bangField($bang)->addCacheList($lists, $KCache, $result, $result);
            }
        }

        return $result;
    }

    // 详情
    public function onShow()
    {
        $opt = $this->option();
        $_id = $opt->getData('id', null, 'intval');
        if(empty($_id)){
            return $this->_returnFail('select_operation_id');
        }

        // 组合数据
        $where = ['id' => $_id];
        $model = new SiteRss();
        $info  = $model->isCache(true)->oneFind($where);
        if($info === false){
            return $this->_returnFail($model->getError());
        }
        if($info['status'] == -1){
            return $this->_returnFail("news_has_no_exist");
        }

        return $info;
    }

    // 软删除
    public function toSoft($myUid)
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('select_operation_id');
        }

        $where  = ['id' => $_id, 'uid' => $myUid];
        $model  = new News();
        $result = $model->isCache(true)->oneEdit($where, ['verify' => -1]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 改变（直接修改，软删除，改变状态，排序）
    public function onEdit($myUid)
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('select_operation_id');
        }

        // 组合参数
        $opt   = $this->option();
        $cates = $opt->getData("cates", [], "is_array");
        $tags  = $opt->getData("tags", [], "is_array");
        if(empty($cates)){
            return $this->_returnFail('select_the_posting_cate');
        }

        $data = [
            //'id' => $opt->getData("id", null, "intval"),
            'uid'            => $myUid,
            'pic'            => $opt->getData("pic", null, "trim"),
            'ip'             => $opt->getIp(),

            // 各个语言的字段
            'en_title'       => $opt->getData("en_title", null, "trim"),
            'cn_title'       => $opt->getData("cn_title", null, "trim"),
            'tc_title'       => $opt->getData("tc_title", null, "trim"),
            'en_keywords'    => $opt->getData("en_keywords", null, "trim"),
            'cn_keywords'    => $opt->getData("cn_keywords", null, "trim"),
            'tc_keywords'    => $opt->getData("tc_keywords", null, "trim"),
            'en_description' => $opt->getData("en_description", null, "trim"),
            'cn_description' => $opt->getData("cn_description", null, "trim"),
            'tc_description' => $opt->getData("tc_description", null, "trim"),
            'en_content'     => $opt->getData("en_content", null, "trim"),
            'cn_content'     => $opt->getData("cn_content", null, "trim"),
            'tc_content'     => $opt->getData("tc_content", null, "trim"),

            // 其他
            'images'         => $opt->getData("images", '[]', "json_decode"),
            'author'         => $opt->getData("author", null, "trim"),
            'verify'         => $opt->getData("verify", 1, "intval"),
            'is_top'         => $opt->getData("is_top", null, "intval"),
            'is_header'      => $opt->getData("is_header", null, "intval"),
            'is_major'       => $opt->getData("is_major", null, "intval"),
            'push_time'      => $opt->getData("push_time", null, "intval"),
            'is_draft'       => $opt->getData("is_draft", 0, "intval"),
            //'read_no'     => $opt->getData("read_no", null, "intval"),
            //'virtual_no'  => $opt->getData("virtual_no", null, "intval"),
            //'praise_no'   => $opt->getData("praise_no", null, "intval"),
            //'collect_no'  => $opt->getData("collect_no", null, "intval"),
            //'relay_no'    => $opt->getData("relay_no", null, "intval"),
            //'comment_no'  => $opt->getData("comment_no", null, "intval"),
            //'ip_address'  => $opt->getData("ip_address", null, "trim"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];


        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('add_the_saved_data');
        }
        if($data === false){
            return $this->_returnFail('add_the_saved_data');
        }
        // 组合数据
        /*$where = ['id' => $_id, 'uid' => $myUid];
        $data  = $model->checkData($data, $where);
        if($data === false){
            return $this->_returnFail('add_the_saved_data');
        }*/

        // 验证
        $model  = new News();
        $result = $model->checkRules($data, 'edit');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 保存
        $result = $model->publish($myUid, $data, $cates, $tags);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 树状
    public function onAdd($myUid)
    {
        // 组合参数
        $opt   = $this->option();
        $cates = $opt->getData("cates", [], "is_array");
        $tags  = $opt->getData("tags", [], "is_array");
        if(empty($cates)){
            return $this->_returnFail('select_the_posting_cate');
        }

        $data = [
            //'id' => $opt->getData("id", null, "intval"),
            'uid'            => $myUid,
            'pic'            => $opt->getData("pic", null, "trim"),
            'ip'             => $opt->getIp(),

            // 各个语言的字段
            'en_title'       => $opt->getData("en_title", null, "trim"),
            'cn_title'       => $opt->getData("cn_title", null, "trim"),
            'tc_title'       => $opt->getData("tc_title", null, "trim"),
            'en_keywords'    => $opt->getData("en_keywords", null, "trim"),
            'cn_keywords'    => $opt->getData("cn_keywords", null, "trim"),
            'tc_keywords'    => $opt->getData("tc_keywords", null, "trim"),
            'en_description' => $opt->getData("en_description", null, "trim"),
            'cn_description' => $opt->getData("cn_description", null, "trim"),
            'tc_description' => $opt->getData("tc_description", null, "trim"),
            'en_content'     => $opt->getData("en_content", null, "trim"),
            'cn_content'     => $opt->getData("cn_content", null, "trim"),
            'tc_content'     => $opt->getData("tc_content", null, "trim"),

            // 其他
            'images'         => $opt->getData("images", '[]', "json_decode"),
            'author'         => $opt->getData("author", null, "trim"),
            'verify'         => $opt->getData("verify", 1, "intval"),
            'is_top'         => $opt->getData("is_top", null, "intval"),
            'is_header'      => $opt->getData("is_header", null, "intval"),
            'is_major'       => $opt->getData("is_major", null, "intval"),
            'push_time'      => $opt->getData("push_time", null, "intval"),
            'is_draft'       => $opt->getData("is_draft", 0, "intval"),
            //'read_no'     => $opt->getData("read_no", null, "intval"),
            //'virtual_no'  => $opt->getData("virtual_no", null, "intval"),
            //'praise_no'   => $opt->getData("praise_no", null, "intval"),
            //'collect_no'  => $opt->getData("collect_no", null, "intval"),
            //'relay_no'    => $opt->getData("relay_no", null, "intval"),
            //'comment_no'  => $opt->getData("comment_no", null, "intval"),
            //'ip_address'  => $opt->getData("ip_address", null, "trim"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];
        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('add_the_saved_data');
        }

        // 组合数据
        $model = new News();
        $data  = $model->checkData($data);
        if($data === false){
            return $this->_returnFail('add_the_saved_data');
        }

        // 验证
        $result = $model->checkRules($data, 'add');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 保存
        //$result = $model->oneAdd($data);
        $result = $model->publish($myUid, $data, $cates, $tags);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

}


